﻿using OxyPlot;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
using Teach1.Gui.Models;

namespace Teach1.Gui.Controls
{
    /// <summary>
    /// WellStructureControl.xaml 的交互逻辑
    /// </summary>
    public partial class WellStructureControl : UserControl
    {
        private WellStructure _Model = new()
        {
            Depth1 = 500,
            Depth2 = 1500,
            HoleSize1 = 12 * 25.4,
            CasingOuterDiameter = 10 * 25.4,
            CasingInnerDiameter = 8.5 * 25.4,
            HoleSize2 = 6 * 25.4
        };

        public WellStructureControl()
        {
            InitializeComponent();
        }


        public WellStructure Model
        {
            get => _Model; 
            set => _Model = value;
        }

        public void Draw()
        {
            const double RATIO = 2000;
            var canvasWidth = canvas.ActualWidth;
            var canvasHeight = canvas.ActualHeight;

            var logicalWidth = Model.HoleSize1 / 1000.0;
            var logicalHeight = Model.Depth2;

            var scaledLogicalHeight = logicalHeight / RATIO;

            var drawingRatio = logicalWidth / scaledLogicalHeight;

            var canvasRatio = canvasWidth / canvasHeight;

            var (drawingWidth, drawingHeight) = (canvasRatio > drawingRatio) ?
               ((canvasHeight - 20) * drawingRatio, canvasHeight - 20)
               : (canvasWidth - 20, (canvasWidth - 20) / drawingRatio);
            canvas.Children.Clear();
            //var rect = new Rectangle
            //{
            //    Fill = new SolidColorBrush(Colors.LightCoral),
            //    Width = drawingWidth,
            //    Height = drawingHeight
            //};
            //Canvas.SetLeft(rect, (canvasWidth - drawingWidth) / 2);
            //Canvas.SetTop(rect, 10);


            //canvas.Children.Add(rect);
            var rect = new Rect((canvasWidth - drawingWidth) / 2, 10, drawingWidth, drawingHeight);

            var scaleX = drawingWidth / logicalWidth;
            var scaleY = drawingHeight / logicalHeight;
            var cx = rect.Left + rect.Width / 2;

            var cementLeft = cx - Model.HoleSize1 /1000.0 / 2 * scaleX;
            var cementRight = cx - Model.CasingOuterDiameter / 1000.0 / 2 * scaleX;
            var cementTop = rect.Top;
            var cementBottom = Model.Depth1*scaleY+ rect.Top;
            var cement = new Rectangle
            {
                Fill = new SolidColorBrush( Colors.DarkGray),
                Width = cementRight - cementLeft,
                Height = Model.Depth1 * scaleY,
            };
            Canvas.SetLeft(cement, cementLeft);
            Canvas.SetTop(cement, cementTop);
            canvas.Children.Add(cement);

            cementRight = cx + Model.HoleSize1 / 1000.0 / 2 * scaleX;
            cementLeft = cx + Model.CasingOuterDiameter / 1000.0 / 2 * scaleX;
            cementTop = rect.Top;
            cementBottom = Model.Depth1 * scaleY + rect.Top;
            cement = new Rectangle
            {
                Fill = new SolidColorBrush(Colors.DarkGray),
                Width = cementRight - cementLeft,
                Height = Model.Depth1 * scaleY,
            };
            Canvas.SetLeft(cement, cementLeft);
            Canvas.SetTop(cement, cementTop);

            canvas.Children.Add(cement);


            var casingLeft = cx - Model.CasingOuterDiameter / 1000.0 / 2 * scaleX;
            var casingRight = cx - Model.CasingInnerDiameter / 1000.0 / 2 * scaleX;
            var casingTop = rect.Top;
            var casingBottom = Model.Depth1 * scaleY + rect.Top;
            var casing = new Rectangle
            {
                Fill = new SolidColorBrush(Colors.DarkBlue),
                Width = casingRight - casingLeft,
                Height = Model.Depth1 * scaleY,
            };
            Canvas.SetLeft(casing, casingLeft);
            Canvas.SetTop(casing, casingTop);
            canvas.Children.Add(casing);

            var holeLeft = cx - Model.HoleSize2 / 1000.0 / 2 * scaleX;

            var l1 = new Line
            {
                Stroke = new SolidColorBrush(Colors.Black),
                StrokeThickness = 1,
                X1 = casingRight,
                Y1 = casingBottom,
                X2 = holeLeft,
                Y2 = casingBottom,
            };
            var l2 = new Line
            {
                Stroke = new SolidColorBrush(Colors.Black),
                StrokeThickness = 1,
                X1 = holeLeft,
                Y1 = casingBottom,
                X2 = holeLeft,
                Y2 = rect.Top + Model.Depth2 * scaleY
            };

            canvas.Children.Add(l1);
            canvas.Children.Add(l2);

            casingRight = cx + Model.CasingOuterDiameter / 1000.0 / 2 * scaleX;
            casingLeft = cx + Model.CasingInnerDiameter / 1000.0 / 2 * scaleX;
            casingTop = rect.Top;
            casingBottom = Model.Depth1 * scaleY + rect.Top;
            casing = new Rectangle
            {
                Fill = new SolidColorBrush(Colors.DarkBlue),
                Width = casingRight - casingLeft,
                Height = Model.Depth1 * scaleY,
            };
            Canvas.SetLeft(casing, casingLeft);
            Canvas.SetTop(casing, casingTop);
            canvas.Children.Add(casing);

            var holeRight = cx + Model.HoleSize2 / 1000.0 / 2 * scaleX;
            l1 = new Line
            {
                Stroke = new SolidColorBrush(Colors.Black),
                StrokeThickness = 1,
                X1 = casingLeft,
                Y1 = casingBottom,
                X2 = holeRight,
                Y2 = casingBottom,
            };
             l2 = new Line
            {
                Stroke = new SolidColorBrush(Colors.Black),
                StrokeThickness = 1,
                X1 = holeRight,
                Y1 = casingBottom,
                X2 = holeRight,
                Y2 = rect.Top + Model.Depth2 * scaleY
            };

            canvas.Children.Add(l1);
            canvas.Children.Add(l2);

        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Draw();
        }
    }
}
